package com.wjh.anyview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.anyview.dao.BankMapper;
import com.wjh.anyview.entity.bo.courseServiceBo.InstitutionGradeBo;
import com.wjh.anyview.entity.dto.CommonResult;
import com.wjh.anyview.entity.po.Bank;
import com.wjh.anyview.entity.vo.examControllerVo.BankVo;
import com.wjh.anyview.service.BankService;
import com.wjh.anyview.service.constant.StringConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjh
 * @date 2021/10/13 20:20
 * @Package com.wjh.anyview.service.impl
 */
@Service
@Slf4j
public class BankServiceImpl implements BankService {
    @Resource
    private BankMapper bankMapper;

    @Override
    public CommonResult<List<BankVo>> listBankByInstitutionGrade(InstitutionGradeBo institutionGradeBo) {
        QueryWrapper<Bank> wrapper = new QueryWrapper<>();
        wrapper.eq("institution_id", institutionGradeBo.getInstitutionId());
        wrapper.eq("grade", institutionGradeBo.getGrade());
        List<Bank> bankList = bankMapper.selectList(wrapper);
        List<BankVo> list = new ArrayList<>();
        bankList.forEach(bank -> {
            BankVo bankVo = new BankVo();
            bankVo.setBankId(bank.getId());
            bankVo.setBankName(bank.getBankName());
            list.add(bankVo);
        });
        return CommonResult.operateSuccess(list);
    }

    @Override
    @Transactional
    public CommonResult<?> saveBank(Bank bank) {
        if (bankMapper.insert(bank) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(StringConstant.SYSTEM_BUSY);
    }

}
